<?php
/**
 * Each engineer has a duty to keep the code elegant
 * Created by wolffy.
 * Author @wolffy<13750088090@sina.cn>
 * Date: 2018/8/14
 * Time: 17:39
 */
namespace App\Http\Controllers\Api\V1_0_1;

use App\Http\Requests\Api\V1_0_1\RepaymentPlanRequest;
use App\Http\Requests\Api\V1_0_1\RepaymentPlanUpdateStatusRequest;
use App\Services\RepaymentPlanService;
use App\Services\UserPassageService;
use Illuminate\Http\Request;

class RepaymentPlanController extends BaseController
{
    protected $repaymentPlanService;
    protected $userPassageService;

    public function __construct(RepaymentPlanService $repaymentPlanService, UserPassageService $userPassageService)
    {
        parent::__construct();
        $this->repaymentPlanService = $repaymentPlanService;
        $this->repaymentPlanService = $repaymentPlanService;
        $this->userPassageService = $userPassageService;
    }

    /**
     * 提交还款计划
     */
    public function confirmRepaymentPlan(RepaymentPlanRequest $repaymentPlanRequest)
    {
        $passageId = 1;
        //是否需要开卡
        $isOpenCard = $this->userPassageService->isHasSubMerchantOpenCard($this->user, $repaymentPlanRequest->id, $passageId);
        if (is_object($isOpenCard)) return $isOpenCard;
        return $this->repaymentPlanService->makePlan($this->user['user_id'], $this->user['mobile'], $repaymentPlanRequest->id, $passageId,
            $repaymentPlanRequest->repayment_plan_data);
    }

    /**
     * 还款计划详情
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function repaymentPlanDetail(Request $request)
    {
        if (empty($request->plan_id)) {
            return $this->responseJson(422);
        }
        return $this->repaymentPlanService->repaymentPlanDetail($request->plan_id, $this->user['user_id']);
    }

    /**
     * 更新计划状态为停止
     */
    public function updatePlanStatusToStop(Request $request)
    {
        if (empty($request->plan_id)) {
            return $this->responseJson(422);
        }
        return $this->repaymentPlanService->updatePlanStatusToStop($request->plan_id, $this->user['user_id']);
    }

}